package com.rcloud.server.sealtalk.service;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.rcloud.server.sealtalk.constant.ErrorCode;
import com.rcloud.server.sealtalk.controller.param.PayParam;
import com.rcloud.server.sealtalk.dao.PayOrderMapper;
import com.rcloud.server.sealtalk.domain.PayOrder;
import com.rcloud.server.sealtalk.domain.Users;
import com.rcloud.server.sealtalk.enums.BusiType;
import com.rcloud.server.sealtalk.exception.ServiceException;
import com.rcloud.server.sealtalk.model.dto.PayInfoDto;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author lgh on 2018/09/15.
 */
@Service
public class PayService {
    @Resource
    private PayOrderMapper payOrderMapper;
    @Resource
    private UsersService usersService;



    /**
     * 不同的订单号，同一个支付流水号
     */
    @Transactional(rollbackFor = Exception.class)
    public PayInfoDto pay(PayParam payParam) throws ServiceException {
        switch (payParam.getBusiType()) {
            //认证费用支付
            case 1:
                return payOrderPayInfo(payParam);
            default:
                return null;
        }
    }
    /**
     * 支付订单支付参数组装
     */
    private PayInfoDto payOrderPayInfo(PayParam payParam) throws ServiceException {
        PayOrder payOrder = payOrderMapper.selectOne(PayOrder.builder().outTradeNo(payParam.getOrderNumber()).build());
        if (payOrder == null) {
            throw new ServiceException(ErrorCode.ALREADY_YOUR_FRIEND);
        }
        PayInfoDto payInfoDto = new PayInfoDto();
        BusiType instance = BusiType.instance(payParam.getBusiType());
        if (instance != null) {
            payInfoDto.setBody(instance.getBusiTypeName());
        }
        //todo 测试价格  0.01 生产价格 payAmount
        payInfoDto.setPayAmount(payOrder.getTradeAmount());
        payInfoDto.setPayNo(payOrder.getOutTradeNo());
        return payInfoDto;
    }


    @Transactional(rollbackFor = Exception.class)
    public void paySuccess(String payNo, String bizType, int payType) throws ServiceException {
        switch (Integer.parseInt(bizType)) {
            //认证费用支付
            case 1:
                payAuthSuccess(payNo, Integer.parseInt(bizType), payType);
                return;
            default:
        }
    }
    /**
     * 1:认证支付
     */
    private void payAuthSuccess(String payNo, int bizType, int payType) throws ServiceException {
        PayOrder payOrder = payOrderMapper.selectOne(PayOrder.builder().outTradeNo(payNo).build());
        if (payOrder == null) {
            throw new ServiceException(ErrorCode.PAY_ORDER_NOT_EXIT_ERROR);
        }
        // 订单已支付
        if (payOrder.getTradeStatus() == 1) {
            throw new ServiceException(ErrorCode.PAY_ORDER_ALREADY_PAY_ERROR);
        }
        // 修改订单信息 (修改支付方式和订单的状态)
        if (payOrderMapper.updateToPay(payNo, payType, payOrder.getVersion()) < 1) {
            throw new ServiceException(ErrorCode.PAY_ORDER_ALREADY_UPDATE_ERROR);
        }
        Integer userId = payOrder.getUserId();
        //具体的业务修改 充值
        if (bizType == 1) {
            //修改用户的支付认证状态
            Users paramUser = new Users();
            paramUser.setId(userId);
            paramUser.setPayAuthStatus(1);
            usersService.updateByPrimaryKeySelective(paramUser);
        }
    }


}
